package iscript.gameFramework.IsoLibarys.display 
{
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import iscript.coreFramework.displays.drawing.LineSegment;
	import iscript.coreFramework.displays.Element;
	import iscript.coreFramework.math.Point2D;
	import iscript.coreFramework.math.Vector2D;
	import iscript.coreFramework.util.GUID;
	import iscript.gameFramework.IsoLibarys.config.IsoConfig;
	import iscript.gameFramework.IsoLibarys.data.IsoBound;
	import iscript.gameFramework.IsoLibarys.IsoView;
	import iscript.gameFramework.IsoLibarys.math.IsoMath;
	import iscript.gameFramework.IsoLibarys.state.IsoSpriteState;

	
	public class IsoSprite extends Sprite implements IIsoDisplayObject
	{
		
		private var _isoview:IsoView;
		private var _isoBound:IsoBound;
	    private var _id:String;
		public  var vars:Object = new Object;		
		private var _groundSprite:Sprite;		 
		private var _state:String;
		private var _content:Sprite;		
		public  var userinfo:Object;
		private var _lock:Boolean;
		private var _isDrag:Boolean;
		
		public function IsoSprite(isoview:IsoView,bound:IsoBound) 
		{
			_id = GUID.create();
			_isoview = isoview;
			_isoBound = bound;
			_groundSprite = new Sprite();	
			addChild(_groundSprite);	
			 
			_content = new Sprite();
			addChild(_content);
			setState(IsoSpriteState.OUTSIDE_VIEW);
			_lock = true;
		}
         
		 
		 
		public function get lock():Boolean { return _lock; }
		public function set lock(value:Boolean):void { _lock = value; }
		
		public function get id():String {return _id; }
		
		/**
		 * 显示内容容器层
		 */
		public function get content():Sprite { return _content; }
		public function getIsoBound():IsoBound { return _isoBound; }
		public function setIsoBound(value:IsoBound):void { _isoBound = value; }
		
		public function setState(value:String):void
		{ 
			if (_lock) return;
				_state = value;
				switch(_state) {
					case IsoSpriteState.OUTSIDE_VIEW:
						 _drawline(IsoConfig.ISOSPRITE_OUTSIDE_COLOR,5)
						//_drawline(0x990000, 5);
					break;
				case IsoSpriteState.INSIDE_VIEW:
						_drawline(IsoConfig.ISOSPRITE_INSIDE_COLOR,5)
					   //drawline(0x000000, 5);
					break;
					}
			 
		}
		
		public function getStage():String { return _state; }
		
		/**
		 * 获得占地向量
		 * @return
		 */
		public function getVector2D():Vector2D {
			 return  IsoMath.getVector2DByBound(_isoview, _isoBound);
			}
		 
		/**
		 * 获得占地矩形
		 * @return
		 */
		public function getGroundRectangle():Rectangle {
			return IsoMath.getIsoBoundRectangle(_isoview, _isoBound);
			}
		
		public function get GroundRectangle():Rectangle {
			return IsoMath.getIsoBoundRectangle(_isoview, _isoBound);
			}
			
		
			
		public function get isoX():Number {
			return  getGroundRectangle().x;
			}		
			
		public function get isoY():Number {
			return  getGroundRectangle().y;
			}	
			
		/**
		 * 允许拖动
		 */
		public function get isDrag():Boolean 
		{
			return _isDrag;
		}
		
		public function set isDrag(value:Boolean):void 
		{
			_isDrag = value;
		}
			
 
		/**
		 * 地面
		 * @return
		 */
		public function getGround():Sprite { return _groundSprite; }	
			
		private function _drawline(color:uint,thickness:int):void{
			var pos:Vector.<Point2D> = IsoMath.getLocalBoundDiamondVertex(_isoview, _isoBound);	
			if (pos == null) return ;
			var r:Rectangle = IsoMath.getLocalBoundRectangle(_isoview, _isoBound);	
			_groundSprite.graphics.clear();
			var line:LineSegment = new LineSegment();
				line.lineColor = color;
				line.fillColor = color;
				line.thickness = thickness;
				//line.drawRectangle(_groundSprite.graphics, r);	
		     	line.drawVectorPoint2D(_groundSprite.graphics, pos);	
			}
			
		public function dispose():void {
			   Element.removeAllItem(this);
			}	
		 
		 
			
	}

}